home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group99a.txt / 000234_icon-group-sender _Thu Nov 18 08:17:23 1999.msg < prev    next >
Internet Message Format  |  2000-09-20  |  3KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id IAA26980
  4.     for icon-group-addresses; Thu, 18 Nov 1999 08:17:13 -0700 (MST)
  5. Message-Id: <199911181517.IAA26980@baskerville.CS.Arizona.EDU>
  6. From: swampler@noao.edu (Steve Wampler)
  7. Date: Wed, 17 Nov 1999 18:03:59 MST
  8. To: Kostas Oikonomou <oikonomou@att.com>
  9. Subject: Re: Strange table behavior
  10. Cc: icon-group@optima.CS.Arizona.EDU
  11. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  12. Status: RO
  13.  
  14. On Nov 17 at  5:48pm, Kostas Oikonomou writes:
  15. } When I run this program
  16. } procedure main()
  17. }   local T
  18. }   T := table([])
  19. }   put(T[1], "A")
  20. }   write("*T = ", *T)
  21. }   write(T[1][1])
  22. } end
  23. } I get
  24. } *T = 0
  25. } A
  26. } Can anyone explain to me what's happening here?  Does it have to do with
  27. } T being a table of lists?
  28.  
  29. I can explain it, but I can't decide if it should be classified as a bug
  30. or a "feature"...(right now I'm leaning toward "feature")
  31.  
  32. First:
  33.  
  34.    T := table([])
  35.  
  36. This creates a table with a specific list as the default value (note,
  37. you probably didn't mean this, see below).  Let's call this particular
  38. list L_001, so we can see what happens later.
  39.  
  40. If you were to ask for the size of the table at this time, you'd
  41. expect to see the value 0, because you haven't assigned anything to
  42. the table yet.
  43.  
  44. Now:
  45.  
  46.    put(T[1], "A")
  47.  
  48. This asks for the value of T[1].  Since you haven't assigned anything
  49. to the table yet, you get back the default value, which is L_001 in
  50. this case.  The string "A" is put into the list L_001, increasing the
  51. size of this list by 1.  *Note that you have not yet assigned anything
  52. to the table!*
  53.  
  54. So:
  55.  
  56.    write("*T = ", *T)
  57.  
  58. outputs the size of the table.  Since nothing has been assigned yet,
  59. it's still 0.
  60.  
  61. Finally:
  62.  
  63.    write(T[1][1])
  64.  
  65. this first gets the value of the element T[1].  Since nothing has been
  66. assigned to the table yet, you get the default value, good old list
  67. L_001.  Then the first element of L_001 is printed, producing the "A".
  68.  
  69. The output is correct, the size of the table has never changed, because
  70. nothing has ever been assigned to the table!  The fact that something
  71. was assigned to the list that was created and used as the default value
  72. for the table doesn't change this fact.
  73.  
  74. Ok, now - why isn't T := table([]) what you want.  Again, breaking it
  75. down:
  76.  
  77.     []          creates a list (L_001, say).
  78.  
  79. table(L_001) says to create a table and make list L_001 the default
  80. value.  Note that it doesn't say "create a new list each time a
  81. new table element is needed".  It says "use the list (L_001)" whenever
  82. you need the default value".  So, for example:
  83.  
  84.     put(T[1], "A")
  85.     put(T[2], "B")
  86.     put(T[3], "C")
  87.     write(T[4][2])
  88.  
  89. would output "B" and
  90.  
  91.     write(*(T["aslkdjfsa"])
  92.  
  93. would output "3"!  (It's the same list being produced as the
  94. default value, regardless of the reference key.)
  95.  
  96. What you want to do is something like:
  97.  
  98.    T := table()
  99.  
  100.    /T[1] := []        # create a brand new list and assign it only to T[1]
  101.                         #     if nothing has ever been assigned to T[1].
  102.    put(T[1], "A")       # put the A into this brand new list
  103.  
  104. I hope this helps!
  105.  
  106. -- 
  107. --
  108. Steve Wampler-  SOLIS Project, National Solar Observatory
  109. swampler@noao.edu
  110.